<%--
  Created by IntelliJ IDEA.
  User: cyc
  Date: 2021/10/2
  Time: 15:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>后台管理 - 易买网</title>
    <link type="text/css" rel="stylesheet" href="../css/style.css" />
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css">
    <script type="text/javascript" src="${pageContext.request.contextPath}/scripts/jquery-1.8.3.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/scripts/jquery.form.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/scripts/function.js"></script>
</head>
<body>

<div class="main">
    <h2>商品管理 <button class="layui-btn" id="addbutton">新增商品</button> </h2>
    <div class="manage">
        <table  id="producttable" lay-filter="usertable">
            <script type="text/html" id="barDemo">

                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>

            </script>

        </table>
    </div>
</div>
<form  class="layui-form" style="display: none;height: 200px;width: 350px" id="productModify" enctype="multipart/form-data" method="post">

    <div class="layui-form-item">
        <label class="layui-form-label">商品名称</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" name="name" lay-verify="required">
            <input type="text" class="layui-input" name="id" style="display: none">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" name="description" lay-verify="required">
        </div>
    </div>



    <div class="layui-form-item">
        <label class="layui-form-label">所属分类</label>
        <div class="layui-input-inline">
            <select name="categorylevel1id" autocomplete="off" lay-verify="required">
                <c:forEach items="${sessionScope.categorylist}" var="i">
                    <option value="${i.id}">${i.name}</option>
                </c:forEach>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">价格</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" name="price" lay-verify="required">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">库存</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" name="stock" lay-verify="required">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">图片</label>
        <div class="layui-input-inline">
            <input type="file" class="layui-input" name="filename" lay-verify="required">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"></label>
        <div class="layui-input-inline">
            <input type="button" class="layui-btn" value="修改" lay-submit="" id="mod_btn">
        </div>
    </div>
</form>

<form  class="layui-form" style="display: none;height: 200px;width: 350px" id="productadd" enctype="multipart/form-data" method="post">

    <div class="layui-form-item">
        <label class="layui-form-label">商品名称</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" name="name" lay-verify="required">

        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" name="description" lay-verify="required">
        </div>
    </div>



    <div class="layui-form-item">
        <label class="layui-form-label">所属分类</label>
        <div class="layui-input-inline">
            <select name="categorylevel1id" autocomplete="off" lay-verify="required">
                <c:forEach items="${sessionScope.categorylist}" var="i">
                    <option value="${i.id}">${i.name}</option>
                </c:forEach>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">价格</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" name="price" lay-verify="required">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">库存</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" name="stock" lay-verify="required">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">图片</label>
        <div class="layui-input-inline">
            <input type="file" class="layui-input" name="filename" lay-verify="required">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"></label>
        <div class="layui-input-inline">
            <input type="button" class="layui-btn" value="添加"  lay-submit="" id="btn_add">
        </div>
    </div>
</form>

</div>
<div id="footer">
    Copyright &copy; 2013 北大青鸟 All Rights Reserved. 京ICP证1000001号
</div>
<script src="${pageContext.request.contextPath}/layui/layui.js"></script>
<script>
    $(function (){


        $("#addbutton").click(function (){
            layer.open({
                type:1,
                title:"添加商品信息",
                content:$("#productadd")
            });
        });


        layui.use("table",function (){
            var table=layui.table;
            table.render({
                elem: '#producttable'
                ,height: 500
                ,url: '${pageContext.request.contextPath}/product/manall' //数据接口
                ,page: true //开启分页
                ,cols: [[ //表头
                    {field: 'id', title: '编号', width:80, fixed: 'left',style:'height:50px;'}
                    ,{field: 'name', title: '商品名称', width:1300,style:'height:50px;'}
                    ,{field: 'filename', title: '图片',templet:function (data){
                            return "<img style='width: 100%;height: 50px' src='${pageContext.request.contextPath}/images/product/"+data.filename+"'>";
                        },style:'height:50px;'}
                    ,{fixed: 'right', width:150, align:'center', toolbar: '#barDemo',style:'height:50px;'}
                ]]
            });

            table.on('tool(usertable)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）

                if(layEvent === 'del'){ //删除
                    $.ajax({
                        type:"post",
                        dataType:"json",
                        url: "${pageContext.request.contextPath}/product/del",
                        data:{"id":data.id},
                        success :function (res){
                            alert(res.msg);
                            table.reload('producttable', {
                            });
                        }
                    });
                } else if(layEvent === 'edit'){ //编辑
                    $("input[name='id']").val(data.id);
                    $("input[name='name']").val(data.name);
                    $("input[name='description']").val(data.description);
                    $("input[name='price']").val(data.price);
                    $("input[name='stock']").val(data.stock);
                    $("option[value="+data.categoryLevel1Id+"]").attr("selected","selected");
                    $("input[name='description']").val(data.description);
                    layer.open({
                        type:1,
                        title:"修改分类信息",
                        content:$("#productModify")
                    });
                } else if(layEvent === 'LAYTABLE_TIPS'){
                    layer.alert('Hi，头部工具栏扩展的右侧图标。');
                }
            });

            //添加商品
            $("#btn_add").click(function (){
                var data=$("#productadd").serialize();
                $("#productadd").ajaxSubmit({
                    type:"post",
                    url:"${pageContext.request.contextPath}/product/add",
                    data:data,
                    dataType: "json",
                    success:function (res){
                        alert(res.msg);
                        table.reload('producttable', {
                        });
                    }
                });
            });

            //修改商品
            $("#mod_btn").click(function (){
                var data=$("#productModify").serialize();
                $("#productModify").ajaxSubmit({
                    type:"post",
                    url:"${pageContext.request.contextPath}/product/update",
                    data:data,
                    dataType: "json",
                    success:function (res){
                        alert(res.msg);
                        table.reload('producttable', {
                        });
                    }
                });
            });


        });

    });

</script>
<style>

    .layui-table-cell{
        text-align:center;
        height: auto;
        white-space: normal;
    }

</style>
</body>
</html>
